    /*********************************************************************************************************/
   /* Foreign Borrowing, Sovereignty, and Public Opinion in the Global South: Traditional Lenders or China? */
  /*********************************************************************************************************/
 /******************************* David Bulman, Ning Leng, and Kerry Ratigan ******************************/
/*********************************************************************************************************/

/* 
This file cleans and reshapes the baseline data for conjoint analysis of bailout loan preferences. It uses the following dataset:
	--- ForeignBorrowingPublicOpinion_CleanData.dta
*/

/* SET DIRECTORY */
cd "C:\Users\dbulman1\Dropbox\Work in progress\Global China Perceptions Survey\Debt\Data and analysis"

/* LOAD DATA */
use ForeignBorrowingPublicOpinion_CleanData.dta, clear

/* Reshape data */

forvalues i=1/5 {
	rename debt_conjoint2_`i' choice`i'
	}

reshape long choice, i(responseid) j(task)
gen rating1=.
gen rating2=.
reshape long rating, i(responseid task) j(loan)
drop rating

gen selected=1 if loan==choice
replace selected=0 if loan!=choice

/* Translate attribute names */

/* Spanish */
forvalues i=1/5 {
	forvalues j=1/4 {
		replace d2c`i'_attrib`j'_name="If your government cannot repay this loan, they will instead repay the lenders with:" if d2c`i'_attrib`j'_name=="Si su gobierno no puede pagar este préstamo, tendrán que pagar a los acreedores con:"
		replace d2c`i'_attrib`j'_name="In order to get this loan, the lenders require your government to:" if d2c`i'_attrib`j'_name=="Para conseguir este préstamo, el acreedor requiere que su gobierno:"
		replace d2c`i'_attrib`j'_name="The loan is provided by:" if d2c`i'_attrib`j'_name=="El préstamo será proporcionado por:"
		replace d2c`i'_attrib`j'_name="The interest rate for this loan is:" if d2c`i'_attrib`j'_name=="La tasa de interés para este préstamo es:" 
		}
	}

/*Indonesian*/
forvalues i=1/5 {
	forvalues j=1/4 {
		replace d2c`i'_attrib`j'_name="If your government cannot repay this loan, they will instead repay the lenders with:" if d2c`i'_attrib`j'_name=="Jika pemerintah Anda tidak dapat membayar kembali pinjaman ini, mereka akan membayar kembali pemberi pinjaman dengan:"
		replace d2c`i'_attrib`j'_name="In order to get this loan, the lenders require your government to:" if d2c`i'_attrib`j'_name=="Untuk mendapatkan pinjaman ini, pemberi pinjaman mengharuskan pemerintah Anda untuk:"
		replace d2c`i'_attrib`j'_name="The loan is provided by:" if d2c`i'_attrib`j'_name=="Pinjaman ini disediakan oleh:"
		replace d2c`i'_attrib`j'_name="The interest rate for this loan is:" if d2c`i'_attrib`j'_name=="Tingkat bunga untuk pinjaman ini adalah:" 
		}
	}

/* Malaysian */	
forvalues i=1/5 {
	forvalues j=1/4 {
		replace d2c`i'_attrib`j'_name="If your government cannot repay this loan, they will instead repay the lenders with:" if d2c`i'_attrib`j'_name=="Jika kerajaan anda tidak dapat membayar balik pinjaman ini, mereka akan sebaliknya membayar balik pemberi pinjaman dengan:"
		replace d2c`i'_attrib`j'_name="In order to get this loan, the lenders require your government to:" if d2c`i'_attrib`j'_name=="Untuk mendapatkan pinjaman ini, pemberi pinjaman memerlukan kerajaan anda untuk:"
		replace d2c`i'_attrib`j'_name="The loan is provided by:" if d2c`i'_attrib`j'_name=="Pinjaman ini disediakan oleh:"
		replace d2c`i'_attrib`j'_name="The interest rate for this loan is:" if d2c`i'_attrib`j'_name=="Kadar faedah untuk pinjaman ini adalah:" 
		}
	}

/* Chinese */
forvalues i=1/5 {
	forvalues j=1/4 {
		replace d2c`i'_attrib`j'_name="If your government cannot repay this loan, they will instead repay the lenders with:" if d2c`i'_attrib`j'_name=="如果你的政府无力偿还该项贷款，后果如下:"
		replace d2c`i'_attrib`j'_name="In order to get this loan, the lenders require your government to:" if d2c`i'_attrib`j'_name=="想要得到这笔贷款，贷款方提出如下附带条款:"
		replace d2c`i'_attrib`j'_name="The loan is provided by:" if d2c`i'_attrib`j'_name=="贷款方为:"
		replace d2c`i'_attrib`j'_name="The interest rate for this loan is:" if d2c`i'_attrib`j'_name=="贷款利率为:" 
		}
	}
	
/* Match and encode project attributes */

gen collateral=""
gen condition=""
gen lender=""
gen intrate=""

forvalues i=1/5 {
	forvalues j=1/4 {
		forvalues k=1/2 {
			replace collateral=d2c`i'_attrib`j'_project`k' if d2c`i'_attrib`j'_name=="If your government cannot repay this loan, they will instead repay the lenders with:" & task==`i' & loan==`k'
			replace condition=d2c`i'_attrib`j'_project`k' if d2c`i'_attrib`j'_name=="In order to get this loan, the lenders require your government to:" & task==`i' & loan==`k'
			replace lender=d2c`i'_attrib`j'_project`k' if d2c`i'_attrib`j'_name=="The loan is provided by:" & task==`i' & loan==`k'
			replace intrate=d2c`i'_attrib`j'_project`k' if d2c`i'_attrib`j'_name=="The interest rate for this loan is:" & task==`i' & loan==`k'
			}
		}
	}

replace intrate="2%" if intrate==".02"
replace intrate="6%" if intrate==".06"
replace intrate="10%" if intrate==".1"
	
/* Translate attributes */
	
	/*Spanish*/
	replace collateral="Natural resources at a below market price" if collateral=="Recursos naturales a un precio inferior al del mercado"
	replace collateral="Exclusive rights to a large area of land" if collateral=="Derechos exclusivos sobre un terreno grande"	
	replace collateral="Nothing, but lose future borrowing opportunities" if collateral=="Nada, pero perderán oportunidades para préstamos futuros"	
	replace condition="Increase transparency in government spending" if condition=="Mejore la transparencia de los gastos gubernamentales"	
	replace condition="Cut government spending on public programs" if condition=="Reduzca los gastos gubernamentales para programas públicos"
	replace condition="No conditions specified" if condition=="Ninguna condición"
	replace lender="The United States government" if lender=="El gobierno de los Estados Unidos"
	replace lender="The Chinese government" if lender==" El gobierno de China"
	replace lender="The Chinese government" if lender=="El gobierno de China"
	replace lender="Private commercial banks" if lender=="Bancos privados comerciales"
	replace lender="The International Monetary Fund (IMF)" if lender=="El Fondo Monetario Internacional (FMI)"
	
	/*Indonesian*/
	replace collateral="Natural resources at a below market price" if collateral=="Sumber daya alam dengan harga di bawah pasar"
	replace collateral="Exclusive rights to a large area of land" if collateral=="Hak eksklusif atas area lahan yang luas"	
	replace collateral="Nothing, but lose future borrowing opportunities" if collateral=="Tidak ada, tetapi kehilangan peluang peminjaman di masa depan"	
	replace condition="Increase transparency in government spending" if condition=="Meningkatkan transparansi dalam pengeluaran pemerintah"	
	replace condition="Cut government spending on public programs" if condition=="Mengurangi pengeluaran pemerintah untuk program publik"
	replace condition="No conditions specified" if condition=="Tidak ada kondisi yang ditentukan"
	replace lender="The United States government" if lender=="Pemerintah Amerika Serikat"
	replace lender="The Chinese government" if lender=="Pemerintah Tiongkok"
	replace lender="Private commercial banks" if lender=="Bank komersial swasta"
	replace lender="The International Monetary Fund (IMF)" if lender=="Dana Moneter Internasional (IMF)"

	/*Malay*/
	replace collateral="Natural resources at a below market price" if collateral=="Sumber asli pada harga di bawah pasaran"
	replace collateral="Exclusive rights to a large area of land" if collateral=="Hak eksklusif ke atas kawasan tanah yang luas"	
	replace collateral="Nothing, but lose future borrowing opportunities" if collateral=="Tiada, tetapi kehilangan peluang pinjaman masa depan"	
	replace condition="Increase transparency in government spending" if condition=="Meningkatkan ketelusan dalam perbelanjaan kerajaan"
	replace condition="Cut government spending on public programs" if condition=="Mengurangkan perbelanjaan kerajaan untuk program awam"
	replace condition="No conditions specified" if condition=="Tiada syarat yang ditetapkan"
	replace lender="The United States government" if lender=="Kerajaan Amerika Syarikat"
	replace lender="The Chinese government" if lender=="Kerajaan China"
	replace lender="Private commercial banks" if lender=="Bank komersial swasta"
	replace lender="The International Monetary Fund (IMF)" if lender=="Tabung Kewangan Antarabangsa (IMF)"

	/*Chinese*/
	replace collateral="Natural resources at a below market price" if collateral=="借款方以低于市场价格获取自然资源"
	replace collateral="Exclusive rights to a large area of land" if collateral=="借款方获得大片土地的专有权"	
	replace collateral="Nothing, but lose future borrowing opportunities" if collateral=="没具体后果，但会失去以后借款的机会"	
	replace condition="Increase transparency in government spending" if condition=="提高政府支出的透明度"
	replace condition="Cut government spending on public programs" if condition=="减少政府在公共项目上的开支"
	replace condition="No conditions specified" if condition=="没有附带条款"
	replace lender="The United States government" if lender=="美国政府"
	replace lender="The Chinese government" if lender=="中国政府"
	replace lender="The Chinese government" if lender==" 中国政府"
	replace lender="Private commercial banks" if lender=="商业银行"
	replace lender="The International Monetary Fund (IMF)" if lender=="国际货币基金组织"
	
	
/* Give shorter labels */
gen collateral2="Land" if collateral=="Exclusive rights to a large area of land"
replace collateral2="Resources" if collateral=="Natural resources at a below market price"
replace collateral2="Nothing" if collateral=="Nothing, but lose future borrowing opportunities"
gen condition2="Transparency" if condition=="Increase transparency in government spending"
replace condition2="Transparency" if condition=="Increase transparency in government spending"
replace condition2="Austerity" if condition=="Cut government spending on public programs"
replace condition2="None" if condition=="No conditions specified"
gen lender2="Private banks" if lender=="Private commercial banks"
replace lender2="Chinese government" if lender=="The Chinese government"
replace lender2="US government" if lender=="The United States government"
replace lender2="IMF" if lender=="The International Monetary Fund (IMF)"
gen intrate2=intrate


/* Numerically encode attributes and specify mapping */
label define labels_collateral ///
           1 "Nothing" ///
           2 "Land" ///
           3 "Resources"
label define labels_condition ///
           1 "None" ///
           2 "Austerity" ///
		   3 "Transparency"
label define labels_lender ///
           1 "IMF" ///
           2 "Chinese government" ///
		   3 "US government" ///
		   4 "Private banks"
label define labels_intrate ///
           1 "2%" ///
           2 "6%" ///
		   3 "10%" 

encode collateral2, gen(att_collateral) label(labels_collateral)
encode condition2, gen(att_condition) label(labels_condition)
encode lender2, gen(att_lender) label(labels_lender)
encode intrate2, gen(att_intrate) label(labels_intrate) 

drop collateral* condition* lender* intrate* *_name *_project1 *_project2	

save ForeignBorrowingPublicOpinion_BailoutLoan.dta, replace
